package de.baumann.browser.fragments

import android.os.Bundle
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.activitys.oc.TransferDetailActivity
import de.baumann.browser.adapter.OcTransferAdapters
import de.baumann.browser.api.net.vo.OcTransfer
import de.baumann.browser.base.BaseOdinFragment
import de.baumann.browser.iview.IOcWalletView
import de.baumann.browser.present.OcWalletPresenter
import kotlinx.android.synthetic.main.fragment_base_oc.*


abstract class BaseOcFragment<T, U> : BaseOdinFragment<IOcWalletView, OcWalletPresenter>(), IOcWalletView {
    //    private lateinit var transferAdapter: OcTransferAdapter
    private lateinit var transferAdapters: OcTransferAdapters
    override fun getLayout(): Int {
        return R.layout.fragment_base_oc
    }

    var page = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        retainInstance = true
    }

    override fun initView(view: View) {
        page = 1
        transferAdapters = OcTransferAdapters()
        ocRvTransfer.adapter = transferAdapters
        transferAdapters.setOnItemClickListener { _, _, position ->
            val transfer = transferAdapters.getItem(position)
            if (transfer != null && transfer.flag != "air") {
                TransferDetailActivity.startTransferDetailActivity(mContext, transfer, getCn())
            }
        }
    }

    override fun initData() {
        if (getCn() == "ODIN") {
            getPresenter().getTransferRecord()
        } else {
            getPresenter().getTransferRecordOC(getCn())
        }
    }

    override fun createPresenter(): OcWalletPresenter? {
        return OcWalletPresenter()
    }

    override fun createView(): IOcWalletView? {
        return this
    }

    override fun getAddress(): String {
        return arguments!!.getString("address")
    }

    private fun getCn(): String {
        return arguments!!.getString("cn")
    }

    override fun getPageNum(): Int {
        return page
    }

    override fun setTransfers(transfers: List<OcTransfer>) {
//        transferAdapter.transfers = transfers
        if (transfers.isEmpty()) {
            ocWLEV.visibility = View.VISIBLE
            ocRvTransfer.visibility = View.GONE
        } else {
            ocWLEV.visibility = View.GONE
            ocRvTransfer.visibility = View.VISIBLE
        }

        if (getPageNum() == 1) {
            transferAdapters.setNewData(transfers as MutableList<OcTransfer>)
        } else {
            transferAdapters.addData(transfers)
        }
        if (transfers.size < 20) {
            transferAdapters.loadMoreEnd()
        } else {
            transferAdapters.setAutoLoadMoreSize(page++)
            initData()
            transferAdapters.loadMoreComplete()
        }
    }

    override fun showBindEth(): Boolean {
        return true
    }

}